<template>
    <TablePage></TablePage>
</template>

<script>
import TablePage from './components/TablePage.vue'
import {sendAxios,sendingAxios} from '../public/packaged_axios'
import { provide, reactive,ref } from 'vue';
export default {
  name: 'App',
  components:{TablePage},
   setup(){
    //小兔儿鲜baseURL
    const BaseUrl = 'https://pcapi-xiaotuxian-front-devtest.itheima.net'
    // banner部分
    let BannerImg = reactive([]);
    sendAxios(BaseUrl+'/home/banner').then(data=>{
      data.result.forEach((e)=>{
        BannerImg.push(e);
      })
    })
    provide('BannerImg',BannerImg)
    // 前台分类
    let CategoryImg = reactive([]);
    sendAxios(BaseUrl+'/home/category/mutli').then(data=>{
      data.result.forEach((e)=>{
        CategoryImg.push(e);
      })
    })
    provide('CategoryImg',CategoryImg)
    // 加载猜你喜欢
    let Goods = reactive([[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]])
    sendAxios(BaseUrl+'/home/goods/guessLike?pageSize=8').then(data=>{
      data.result.items.forEach((e)=>{
        Goods[0].push(e);
      })
    })
    provide('Goods',Goods)
    // 加载更多猜你喜欢
    let axiosData = null
    function GetMoreGoods(number){
      axiosData=sendingAxios(BaseUrl+'/home/goods/guessLike?pageSize=8&page='+number)
      if(axiosData){
        axiosData.then(data=>{
        data.result.items.forEach((e)=>{
        Goods[0].push(e);
        })
      })
      return true
      }else{
        return false
      }
    }
    provide('GetMoreGoods',GetMoreGoods)
    //加载热门推荐
    function HotGoods(index,url){
      sendAxios(BaseUrl+url+'?pageSize=8').then(data=>{
      data.result.subTypes.forEach((e)=>{
        Goods[index].push(e);
      })
      })
    }
    //加载特惠推荐
    HotGoods(1,'/hot/preference');
    //爆款推荐
    HotGoods(2,'/hot/inVogue');
    //一站全买
    HotGoods(3,'/hot/oneStop');
    //新鲜好物
    HotGoods(4,'/hot/new');
    //加载更多特惠推荐
    let HotData = null
    function GetMoreHotGoods(index,url,number){
      HotData=sendingAxios(BaseUrl+url+'?pageSize=8&page='+number)
      if(HotData){
        let SubNumber = 0
        HotData.then(data=>{
        data.result.subTypes.forEach((e)=>{
          e.goodsItems.items.forEach((i)=>{
            Goods[index][SubNumber].goodsItems.items.push(i)
          })
          SubNumber++
        })
      })
      return true
      }else{
        return null
      }
    }
    provide('GetMoreHotGoods',GetMoreHotGoods)
    //加载分类
    sendAxios(BaseUrl+'/category/top').then(data=>{
    let number = 5
    data.result.forEach((e)=>{
      Goods[number].push(e)
      number++
    })
    })
    //加载更多搜索结果
    let SearchData = null
    function GetSearchGoods(keyword,number){
      SearchData=sendingAxios(`https://api-hmugo-web.itheima.net/api/public/v1/goods/search?query=${keyword.value}&pagenum=${number}`)
      if(SearchData){
        SearchData.then(data=>{
          data.message.goods.forEach((e)=>{
            Goods[16].push(e)
          })
        })
        return true
      }else{
        return false
      }
    }
    provide('GetSearchGoods',GetSearchGoods)
    //得到本地存储的数据
    let RegisterImformation = reactive(eval(localStorage.getItem("Register")))
    provide('RegisterImformation',RegisterImformation);
    //登录账号的序号
    let LoginIndex = ref(null)
    provide('LoginIndex',LoginIndex)
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
